<?php
// 系统默认控制器
class CustomAction extends SaleAction
{
    public function index()
    {
        /*--------------------------------------------------*/
        $customDay=C("customDay");
        $custtypes=C("custtypes");

        $remarktable=M('remarks');
        $lastestid=$remarktable->field('DISTINCT customid')->where('lxtime>=DATE_SUB(CURDATE(), INTERVAL '.$customDay.' DAY) and adduser !=0 ')->select();
        $lastestids="";
        foreach ($lastestid as $kv => $vv)
        {
            $lastestids .= $vv["customid"] . ",";
        }
        $lastestids = substr($lastestids, 0, -1);
        $customtable=M("custom");
        $customtable->where('id not in('.$lastestids.') and adduser !=0 and (TO_DAYS(NOW())>TO_DAYS(addtime)+ 1) and level not in ('.$custtypes.') ')->setField('adduser','0');
        //dump($customtable->getLastSql());exit;
        /*---------------------------------------------------*/


        $padduser=I("adduser",0); //------------------查询的员工
        //$this->adduser=I("adduser",I("session.userid"));
        $this->adduser=I("adduser",$_SESSION['my_info']['id']);

        //dump($_SESSION);

        $inter=I("get.interval",-1);

        $typeids='0,'.I("session.userid");
        $condition['adduser']=array('exp',"in ( $typeids )");
        $data=$this->getData("custtype",'id,name',$condition);
        $this->custtype_list=$data;
        unset($condition);

        Load('extend');
        $userid = I("session.userid");
        if( $padduser!='-1')
           $condition["adduser"] = $userid;
        $this->uid=$userid;

        $empids = $this->getMyEmployee(); //--------------------------下属员工
        if (!empty($padduser) && $padduser!='-1')
        {
            if (!strstr($empids, $padduser))
            {
                $this->error("你不能查看用户的客户");
            }
            $condition['adduser'] = $padduser;
            switch ($inter)
            {
                case '0':
                    $condition['_string'] = "to_days(addtime)=to_days(now())";
                    break;
                case '-1':
                    $condition['_string'] = " TO_DAYS(NOW())=TO_DAYS(addtime)+ 1";
                    break;
                case '-7':
                    $condition['_string'] = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(addtime) ";
                    break;
                default :
            }
        }

        $emp_con['id'] = array('in', $empids);
        $data = $this->getData('employee', 'id,name', $emp_con);
        $this->emp_list = $data;
        unset($data);
        unset($emp_con); //------------------列出下属员工


        $con_nodate=$condition;
        unset($con_nodate["_string"]);
        $remarkdata = $this->CacheArray("remarks", 'customid',$con_nodate);
        $this->remarkdata = $remarkdata;

        $page = I("get.p");
        $level = I('get.level', 0);


        switch ($level)
        {
            case "-1":
            case "0":
                break;
            case "1": //---------------------------------------------普通客户
                $condition['level'] = array("elt", $level);
                break;
            default :
                $condition['level'] = array("eq", $level);
                break;
        }
         
        $st=I("get.st",'1970-1-1');
        $et=I("get.et",date("Y-m-d H:i:s"));    

        if($st=="")
            $st='1970-1-1';
        if($et=="")
            $et= date("Y-m-d H:i:s");

        if(!empty($condition['_string']))
            $condition['_string'].=" and ";
        $condition['_string'].=" to_days(addtime)>=to_days('$st') and to_days(addtime)<=to_days('$et')";
        
        $remark=I("get.remark",0);
        
        $tempEmployees= $this-> getMyEmployee(0);
        foreach ($tempEmployees as $k => $v)
            $employees[$v["id"]] = $v;
        $this->employees=$employees;

        //dump($condition);exit;

        $mark =I("get.mark",1);
        $keywords = I("get.keywords") ;
        $this->mark=$mark;
        if (!mb_check_encoding($keywords, 'utf-8')){
            $keywords = iconv('gb2312', 'utf-8', $keywords);
        }
        $this->keywords=$keywords;
        $tablename="custom";
        switch($mark)
        {
            case '0':
                $ziduan = 'cname';
                break;
            case '1':
                $ziduan = 'company';
                break;
            case '2':
                $ziduan = 'tel';
                break;
            case '3':
                $ziduan = 'address';
                break;
            case '4':
                $tablename="RemarkView";
                $ziduan = 'content';
                break;
        }
        $empids = $this->getMyEmployee();
        $condition[$ziduan] = array("exp", "like '%$keywords%'");
        $con1["adduser"] = array("exp", "in ($empids)");

        $con = array_merge($con1, $condition);
        //$data = $this->getCustom($page, $condition);
        $data = $this->getPageData($tablename, $page, $con,"addtime desc");
        $this->assign('accounts_list', $data["list"]); // 数据列表变量赋值
        $this->assign('page_method', $data['pageshow']); //分页列表变量赋值
        $this->display(); // 输出模板
    }

    public function ExportCustoms()
    {
    	$custom=M("custom");
    	$padduser=I("adduser",0); //------------------查询的员工
        $inter=I("get.interval",-1);

        Load('extend');
        $userid = I("session.userid");
        if( $padduser!='-1')
           $condition["adduser"] = $userid;

        $empids = $this->getMyEmployee();//--------------------------下属员工
        if (!empty($padduser)&& $padduser!='-1')
        {
            if (strstr($empids, $padduser))
            {
                $condition['adduser'] = $padduser;
            } else
            {
                $empid = I("get.empid", 0);
                if (!empty($empid))
                {
                    if (strstr($empids, $empid) == false)
                    {
                        $this->error("你不能查看用户的客户");
                    }
                    $condition["adduser"] = $empid;
                }
            }

        }

        $con_nodate=$condition;
        unset($con_nodate["_string"]);
        $remarkdata = $this->CacheArray("remarks", 'customid',$con_nodate);
        
        $level = I('get.level', 0);
        switch ($level)
        {
            case "-1":
            case "0":
                break;
            case "1": //---------------------------------------------普通客户
                $condition['level'] = array("elt", $level);
                break;
            default :
                $condition['level'] = array("eq", $level);
                break;
        }
        
        $st=I("get.st",'1970-1-1');
        $et=I("get.et",date("Y-m-d H:i:s"));    

        if($st=="")
            $st='1970-1-1';
        if($et=="")
            $et= date("Y-m-d H:i:s");
       
        $condition['_string'].=" to_days(addtime)>=to_days('$st') and to_days(addtime)<=to_days('$et')";
        
        $con1 = array();
        $emps = $this->getMyEmployee();
        $con1["adduser"] = array("exp", "in($emps)");

        $mark =I("get.mark",1);
        $keywords = I("get.keywords") ;
        $this->mark=$mark;
        if (!mb_check_encoding($keywords, 'utf-8')){
            $keywords = iconv('gb2312', 'utf-8', $keywords);
        }
        $this->keywords=$keywords;
        $tablename="custom";
        switch($mark)
        {
            case '0':
                $ziduan = 'cname';
                break;
            case '1':
                $ziduan = 'company';
                break;
            case '2':
                $ziduan = 'tel';
                break;
            case '3':
                $ziduan = 'address';
                break;
            case '4':
                $tablename="RemarkView";
                $ziduan = 'content';
                break;
        }
        $condition[$ziduan] = array("exp", "like '%$keywords%'");
        $con = array_merge($con1, $condition);

        $tempData=D($tablename)->field("id,adduser,cname,company,tel,userproduct,addtime")->where($con)->order('addtime desc')->select();

        $tempEmployees= $this-> getMyEmployee(0);
        foreach ($tempEmployees as $k => $v)
            $employees[$v["id"]] = $v;
        
        import("@.ORG.Util.Export");
        
        $title=array("负责人","公司名称","联系电话","公司产品",  "业务备注","录入人","最后联系",  "添加日期");             
        
        
       // dump($tempData);exit;
        foreach ($tempData as $k => $v)
        {
        	$ExportData[$k]["cname"] = $v["cname"];
        	$ExportData[$k]["company"] = $v["company"];
        	$ExportData[$k]["tel"] = $v["tel"];
        	$ExportData[$k]["userproduct"] = $v["userproduct"];
        	//业务备注
            $rcontent=$remarkdata[$v['id']]['content'];
            $ExportData[$k]["remark"]=$rcontent;
            //录入人
            $ExportData[$k]["rname"]=$employees[$v['adduser']]['name'];

            $rlxtime=$remarkdata[$v['id']]['lxtime'];
            $ExportData[$k]["rlxtime"]=$rlxtime;
            
        	$ExportData[$k]["addtime"] = $v["addtime"];
        }
        
        
        //dump($ExportData);exit;
        
        
        exportexcel($ExportData,$title,"客户信息表");

    }
    
    public function customnum()
    {
        $interval = I("get.interval",0);
        $custom = M('custom');
        $employeeIds = $this->getMyEmployee();

        $showtitle="";
        $remarksql="select adduser,count(*) as renum from ys_remarks where 1=1 ";
        $sql = "select adduser, count(*) as  CustomNum,rname from ys_custom ";
        $sql .= "where 1=1  ";
        $casesql="";
        switch ($interval)
        {
            case 0:
                $showtitle="今天";
                $casesql = "and to_days(addtime)=to_days(now())";
                $remarksql.="and to_days(lxtime)=to_days(now())";

                $focus_sql="SELECT count(*) as fcs ,adduser  from ys_remarks
            where customid not in
            (
             SELECT id  from ys_custom
            where TO_DAYS(addtime)=TO_DAYS(now())

            )   and TO_DAYS(lxtime)=TO_DAYS(NOW())
            GROUP BY adduser";
                $focus_data=M()->query($focus_sql);
                $fcs_arr=array();
                foreach($focus_data as $k=>$v)
                {
                    $fcs_arr[$v['adduser']]=$v['fcs'];
                }


                break;
            case -1:
                $showtitle="昨天";
                $casesql = "and  TO_DAYS(NOW())=TO_DAYS(addtime)+ 1 ";
                $remarksql.= "and  TO_DAYS(NOW())=TO_DAYS(lxtime)+ 1 ";
                break;
            case -7:
                $showtitle="近7天";
                $casesql = "and  DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(addtime) ";
                $remarksql.="and  DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(lxtime) ";
                break;
        }
        $sql.=$casesql;
        $sql .= "and adduser in($employeeIds) ";
        $sql .= "GROUP BY adduser";


        $remarksql.=" group by adduser ";

        $remark_ar=array();
        $remark_data=M("remarks")->query($remarksql);
        foreach($remark_data as $k=>$v)
        {
            $remark_ar[$v['adduser']]=$v;
        }

        $emp_ar=$this->CacheArray("employee");

        $showtitle.="录入的客户数量";
        $this->showtitle=$showtitle;
        $todaylist = $custom->query($sql);




        foreach ($todaylist as $kt => &$vt)
        {
            $vt["remarks"] = 0;
            $vt["focus"] = 0;

            $vt['rname']=$emp_ar[$vt['adduser']]['name'];
            if (isset($remark_ar[$vt['adduser']]["renum"]))
            {
                $vt["remarks"] = $remark_ar[$vt['adduser']]["renum"];
            }
            if(isset($fcs_arr[$vt['adduser']]))
            {
                $vt["focus"]=$fcs_arr[$vt['adduser']];
            }

        }
        //------------------------------------加入备注数量



        $this->assign("list", $todaylist);
        $this->inter=$interval;
        $this->display();
    }

    public  function Remarks()
    {

        $padduser=I("adduser",""); //------------------查询的员工
        $inter=I("get.interval",0);
        $focus=I("get.type","");//----------------跟踪客户

        $this->producttype = $this->CacheArray("producttype");
        $this->custtype = $this->CacheArray("custtype");
        Load('extend');
        $userid = I("session.userid");
        $condition["adduser"] = $userid;

        $empids = $this->getMyEmployee();//--------------------------下属员工
        if (!empty($padduser))
        {
            if (strstr($empids, $padduser))
            {
                $condition['adduser'] = $padduser;//radduser 是评论中的添加人

                switch ($inter)
                {
                    case '0':
                        $condition['_string'] = "to_days(lxtime)=to_days(now())";

                        break;
                    case '-1':
                        $condition['_string'] = " TO_DAYS(NOW())=TO_DAYS(lxtime)+ 1";
                        break;
                    case '-7':
                        $condition['_string'] = "DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(lxtime) ";
                        break;
                }
                if ($focus == 'focus')
                {
                    unset($condition);
                    $condition['_string'] = " to_days(lxtime)=to_days(now()) and r.adduser='$padduser'
                     and customid not in
                     (
                    SELECT id  from ys_custom where TO_DAYS(addtime)=TO_DAYS(now()) and adduser='$padduser'
                    )";
                }

            } else
            {
                $empid = I("get.empid", 0);
                if (!empty($empid))
                {
                    if (strstr($empids, $empid) == false)
                    {
                        $this->error("你不能查看用户的客户");
                    }
                    $condition["adduser"] = $empid;
                }
            }

        }

        $remarkview=D("RemarkView");
        $data=$remarkview->where($condition)->select();

        $this->assign('accounts_list', $data); // 数据列表变量赋值
        $this->display();
    }

    protected function  data2Ary($tabledata, $id, $val)
    {
        $ary = array();
        foreach ($tabledata as $k => $v)
        {
            $ary[$v[$id]] = $v[$val];
        }
        return $ary;
    }

    public  function  chart()
    {

        $model = new Model();
        $day=I("get.day");
        $adduser=I("get.adduser","");

        $data = $this->getMyEmployee(0);
        $empids=$this->getMyEmployee();
        $this->assign("emp_list", $data);

        $field="SELECT HOUR(addtime) as hours,count(*) as hnum";
        $where=" and TO_DAYS(addtime)=TO_DAYS(NOW()) ";

        if (!empty($day))
        {
            $where = " and TO_DAYS(addtime)=TO_DAYS('$day') ";
        }
        if (!empty($adduser))
        {
            $where .= " and  adduser=$adduser";
        }else
        {
            $where .= " and  adduser in($empids)";
        }


        $sql = $field . " from ys_custom where 1=1";
        $sql .= $where;
        $sql .= "  GROUP BY HOUR(addtime)";
        $data = $model->query($sql);
       // echo $sql."<br />";


        $marksql = str_replace("addtime", "lxtime", $field) . " from ys_remarks where 1=1";
        $marksql .= str_replace("addtime", "lxtime", $where);
        $marksql .= " GROUP BY HOUR(lxtime)";
        $mark_tb_data = $model->query($marksql);
       // echo $marksql."<br />";




        $catedata="";
        $category="";
        $markdata="";

        $hourar=$this->data2Ary($data,'hours','hnum');
        $markar=$this->data2Ary($mark_tb_data,'hours','hnum');



        for ($i = 7; $i < 21; $i++)
        {
            $num = 0;
            $marknum=0;
            if (isset($hourar[$i]))
            {
                $num = $hourar[$i];
            }
            if(isset($markar[$i]))
            {
                $marknum=$markar[$i];
            }
            $catedata .= sprintf("%d,", $num);
            $markdata .= sprintf("%d,", $marknum);
            $category .= sprintf('"%d点",', $i);
        }

        $catedata=substr($catedata,0,-1);
        $category=substr($category,0,-1);
        $markdata=substr($markdata,0,-1);


        $this->assign("catedata",$catedata);
        $this->assign("category", $category);
        $this->assign("markdata", $markdata);
        $this->display();
    }

    public function boss()
    {
        $account = D('Custom'); // 实例化模型类


        $zcount = $account->count();
        $this->assign('zcount', $zcount);

        $acount = $account->where("level='A'")->count();
        $this->assign('acount', $acount);

        $bcount = $account->where("level='B'")->count();
        $this->assign('bcount', $bcount);

        $yuyue = M('Appointment');
        $yycount = $yuyue->where("TO_DAYS(addtime) >= TO_DAYS(NOW())")->count();
        $this->assign('yycount', $yycount);


        $count = $account->count();
        //分页显示文章列表，每页20篇文章
        import('ORG.Util.Page');
        $page = new Page($count, 20); //后台管理页面默认一页显示8条文章记录
        $page->setConfig('prev', "上一页"); //上一页
        $page->setConfig('next', '下一页'); //下一页
        $page->setConfig('first', '首页'); //第一页
        $page->setConfig('last', '尾页'); //最后一页
        $page->setConfig('theme', ' %first% %upPage%  %linkPage%  %downPage% %end%');
        //设置分页回调方法
        $show = $page->show();
        $firstpage = $page->firstRow;
        $lastpage = $page->listRows;

        $list = $account->query("select *, (select content from ys_remarks where ys_remarks.customid = ys_custom.id ORDER BY rid desc limit 0,1) as `content` from ys_custom  order by ys_custom.id desc limit $firstpage,$lastpage");
        //$account->getlastsql();
        $this->assign('accounts_list', $list); // 数据列表变量赋值
        $this->assign('page_method', $show); //分页列表变量赋值
        $this->display("bossindex"); // 输出模板
    }

    public function add()
    {
    	$area=M("areas");
    	$areaList=$area->where(" parent_id='291'")->select();
    	$this->areaList=$areaList;
    	
    	//如果是编辑读取客户信息
    	$id = $_GET['id'];
    	if($id!="")
    	{  
    	 	 $this->action = "edit";
    	     $custom = M('Custom');
             $custom_item = $custom->where("id=$id")->find();
             $this->custom = $custom_item;
    	}
        else 
            $this->action = "add";
       
    	
        $this->productlist = $this->getData('producttype', 'id,name');
       // $ids="0,".I('session.userid');
      //  $typcon["adduser"]=array("in",$ids );
        $this->custtypelist = $this->getData("custtype", "id,name");
       
        $this->display();
    }

    public function checkName()
    {
    	$custom=M('Custom');
    	$company=I("post.company");
    	$id=I("post.id");

        $tmpcompany=$company;
        $tmpcompany=str_replace("山东省","",$tmpcompany);
        $tmpcompany=str_replace("山东","",$tmpcompany);
        $tmpcompany=str_replace("聊城市","",$tmpcompany);
        $tmpcompany=str_replace("聊城","",$tmpcompany);
        $tmpcompany=str_replace("东昌府区","",$tmpcompany);
        $tmpcompany=str_replace("东昌府","",$tmpcompany);
        $tmpcompany=str_replace("临清市","",$tmpcompany);
        $tmpcompany=str_replace("临清","",$tmpcompany);
        $tmpcompany=str_replace("阳谷县","",$tmpcompany);
        $tmpcompany=str_replace("阳谷","",$tmpcompany);
        $tmpcompany=str_replace("莘县","",$tmpcompany);
        $tmpcompany=str_replace("茌平县","",$tmpcompany);
        $tmpcompany=str_replace("茌平","",$tmpcompany);
        $tmpcompany=str_replace("东阿县","",$tmpcompany);
        $tmpcompany=str_replace("东阿","",$tmpcompany);
        $tmpcompany=str_replace("冠县","",$tmpcompany);
        $tmpcompany=str_replace("高唐县","",$tmpcompany);
        $tmpcompany=str_replace("高唐","",$tmpcompany);
        $tmpcompany=str_replace("有限公司","",$tmpcompany);
        $tmpcompany=str_replace("公司","",$tmpcompany);

        if(empty($tmpcompany))
        {
            $info="<br>您录入的信息不完整";
            echo '{"status":"false","errMsg":"'.$info.'"}';exit;
        }

    	$theUser=$custom->field("id,rname,company,addtime,level")->where("company like '%$tmpcompany%' ")->select();
    	 
        if($theUser == null) {
            echo  '{"status":"success","errMsg":""}';
        }else{
        	        	
        	$info="<br>和以下人员撞单：";
            $info.="<table>";
	        foreach ($theUser as $k => $v)
	        {
	            //如果客户的ID等于搜索出来撞单客户的ID 则不拼接
                $info.="<tr>";

	        	if($v["id"]!=$id)
	        	{
	               $rnames.=$v['rname']."&nbsp;";
                    $info.="<td>".$v['rname'].":</td><td>".$v['company']."</td>";
	        	}

                $info.="</tr>";
	        }
            $info.="</table>";
            if($rnames=="")  
	        	echo  '{"status":"success","errMsg":""}';
	        else 
	        {
                echo  '{"status":"false","errMsg":"'.$info.'"}';
	        }
        }
    }

    public function checktel()
    {
        $custom=M('Custom');
        $tel=I("post.tel");
        $id=I("post.id");
        $theUser=$custom->field("id,rname,company,addtime,level,tel")->where("tel = '$tel' ")->select();
        // echo  '{"status":"success","errMsg":"'.$id.'"}';

        if(empty($tel))
        {
            echo  '{"status":"success","errMsg":""}';
        }


        if($theUser == null) {
            echo  '{"status":"success","errMsg":""}';
        }else{

            $info="<br>和以下人员撞单：";
            $info.="<table>";
            foreach ($theUser as $k => $v)
            {
                $info.="<tr>";
                //如果客户的ID等于搜索出来撞单客户的ID 则不拼接
                if($v["id"]!=$id)
                {

                    $rnames.=$v['rname']."&nbsp; ";
                    $info.="<td>".$v['rname'].":</td><td>".$v['company']."</td><td>".$v["tel"]."</td>";
                }
                $info.="</tr>";
            }
            $info.="</table>";

            if($rnames=="")
                echo  '{"status":"success","errMsg":""}';
            else
            {
                echo  '{"status":"false","errMsg":"'.$info.'"}';
            }
        }

    }
    
    public function save()
    {
        $newuser = D('Custom'); // 实例化模型类
        $data = $newuser->create();
        if (!$data) $this->error($newuser->getError());
        $myinfo = I("session.my_info");

        $custominfo = $newuser->find($data['id']);
        if($custominfo!="")
        {
	         if ($custominfo['adduser'] != $myinfo["id"])
	        {
	            $this->error("你不能编辑当前用户");
	        }
        }
        
        $data["adduser"] = $myinfo["id"];

        $data["rname"] = $myinfo["name"];
        $data["section"] =$myinfo["department"];

        $data['level'] = I('post.level');
        
        $data["area_id"]=I('post.area');
        $area=M("areas");
    	$area_names=$area->where(" area_id='".$data['area_id']."'")->find();
        if($area_names!=null)
        {  
           $data["area_name"]=$area_names["area_name"];
        }
        
        $action=I('post.action');
        if($action=="add")
        {
            //录入人ID
            $data["inputer"] = $myinfo["id"];
	         if (!$newuser->add($data))
	        {
	            $this->error("提交失败");
	        }
        }
        else 
        {   
        	if(!$newuser->save($data))
	        {
	            $this->error("提交失败");
	        }
        }
        
		 
       $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script>
	   <script type="text/javascript">
	   $(function(){
		   top.Dialog.alert("成功提交",function(){
			   window.parent.frmright.location.reload();

			   window.location=" '. U('add') .' ";
	   })})</script>
	   ', 'utf-8');

    }

    
    public function edit()
    {
        $id = I('get.id');
        $account = M('Remarks'); // 实例化模型类
        $con['customid'] = $id;

        import("ORG.Util.Page");
        $pagsize = 15;
        $totalnum = $account->where($con)->count();
        $list = $account->where($con)->page($_GET['p'])->order('id desc')->limit()->select();

        $this->assign('customid', $id);
        $this->assign("accounts_list", $list);

        $page = new Page($totalnum, $pagsize);
        $page->rollPage = $pagsize;
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("pageshow", $pageshow);
        $this->display();
    }

    public function editsave()
    {
        $username = I("session.userid"); //获取session的用户名
        $newmark = D('Remarks'); // 实例化模型类
        $data=$newmark->create();
        if(!$data)
        {
            $this->error($newmark->getError());
        }
        $data['adduser'] = $username;
        $data['customid'] = $_GET['id'];
        $newmark->add($data);
        $this->show('
        <script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script>
        <script type="text/javascript">
        $(function(){top.Dialog.alert("成功提交",function(){
        window.parent.frmright.location.reload();top.Dialog.close()
        })})</script>', 'utf-8');
    }

    public function customgiveup()
    {
        $id = I("get.id");
        $custom = M('Custom');
        $data = array('adduser' => "0");
        $custom->where("id=$id")->save($data); // 根据条件保存修改的数据
        $this->success('放弃成功');
    }


    public function _empty()
    {
        $this->error("there is no this function");
    }

    public function level()
    {
        $username = I("session.userid");
        $account = D('Custom'); // 实例化模型类
        $count = $account->count();
        //分页显示文章列表，每页20篇文章
        import('ORG.Util.Page');
        $page = new Page($count, 20); //后台管理页面默认一页显示8条文章记录
        $page->setConfig('prev', "上一页"); //上一页
        $page->setConfig('next', '下一页'); //下一页
        $page->setConfig('first', '首页'); //第一页
        $page->setConfig('last', '尾页'); //最后一页
        $page->setConfig('theme', ' %first% %upPage%  %linkPage%  %downPage% %end%');
        //设置分页回调方法
        $show = $page->show();
        //$list=$account->join('ys_remarks on ys_custom.id=ys_remarks.customid')->GROUP('ys_custom.id')->order('ys_remarks.id desc')->limit($page->firstRow.','.$page->listRows)->select();// 查询数据
        $firstpage = $page->firstRow;
        $lastpage = $page->listRows;
        $list = $account->query("select *, (select lxtime from ys_remarks where ys_remarks.customid = ys_custom.id ORDER BY rid desc limit 0,1) as `lxtime`, (select content from ys_remarks where ys_remarks.customid = ys_custom.id ORDER BY rid desc limit 0,1) as `content` from ys_custom where ys_custom.adduser = '$username' and ys_custom.level!='C'  order by ys_custom.id desc limit $firstpage,$lastpage");


        //$account->getlastsql();
        $this->assign('accounts_list', $list); // 数据列表变量赋值
        $this->assign('page_method', $show); //分页列表变量赋值
        $this->display('index'); // 输出模板
    }

    public function publist()
    {
        Load('extend');
        import("ORG.Util.Page");
        $pagsize = 25;
        $custom = D('Custom'); // 实例化模型类
        $condtion["adduser"] = 0;
        $totalnum = $custom->where($condtion)->count();
        //分页显示文章列表，每页20篇文章


        $remarkdata = $this->CacheArray("remarks", 'customid');
        $this->remarkdata = $remarkdata;

        $list = $custom->where($condtion)->page($_GET['p'])->order('id desc')->limit($pagsize)->select();
        $this->assign("accounts_list", $list);
        $page = new Page($totalnum, $pagsize);
        $page->rollPage = $pagsize;
        $page->setConfig("prev", "上一页");
        $page->setConfig("next", "下一页");
        $page->setConfig("first", "首页");
        $page->setConfig("last", "末页");
        $page->setConfig("theme", "共%totalRow%%header%条数据 %nowPage%/%totalPage%页  %first% %upPage%   %linkPage%  %downPage% %end%");
        $pageshow = $page->show();
        $this->assign("page_method", $pageshow);
        $this->display();
    }

    public function search()
    {
        import("ORG.Util.Page");
        Load('extend');
        
        $mark =I("get.mark");
        $key = I("get.key") ;
        
        $this->mark=$mark;
        $this->keywords=$key;

        if (!mb_check_encoding($key, 'utf-8')){
				$key = iconv('gb2312', 'utf-8', $key);
	    }
/*        $this->assign("key",$key);*/
        
        $level = I("get.level", 0);
        $this->level = $level;
        $pagenum = I('get.p');

        $condition=array();
        $tablename="custom";
        switch($mark)
        {
            case '0':
                $ziduan = 'cname';
                break;
            case '1':
                $ziduan = 'company';
                break;
            case '2':
                $ziduan = 'tel';
                break;
            case '3':
                $ziduan = 'address';
                break;
            case '4':
                $tablename="RemarkView";
                $ziduan = 'content';
                break;
        }
        $empids = $this->getMyEmployee();
        $condition[$ziduan] = array("exp", "like '%$key%'");
        $condition["adduser"] = array("exp", "in ($empids)");
        if(I("get.publist", 0))
            $condition["adduser"] = array("exp", "in (0)");


        $remarkdata = $this->CacheArray("remarks", 'customid',$condition);
        $this->remarkdata = $remarkdata;

        $tempEmployees= $this-> getMyEmployee(0);
        foreach ($tempEmployees as $k => $v)
            $employees[$v["id"]] = $v;
        $this->employees=$employees;


        $data = $this->getPageData($tablename, $pagenum, $condition);

        $this->assign("accounts_list", $data['list']);
        $this->assign("page_method", $data["pageshow"]);
        if(I("get.publist", 0))
        {
            $this->display("publist");
        }
        else
        $this->display("index"); // 输出模板
    }

    public function publicsave()
    {
        $id = I("get.id");
        $username = I("session.userid");
        $realname = I("session.realname");
        $custom = M('Custom');
        $data = array('adduser' => $username,'rname'=>$realname,'updatetime'=>date("Y-m-d H:i:s"),'addtime'=>date("Y-m-d H:i:s"));
        $custom->where("id=$id")->setField($data); // 根据条件保存修改的数据
        $this->success('挑入成功');
    }

    public function pagesizesave()
    {
        $uid = I("session.userid");
        $pagesize = I('post.pagesize',3);
        $employee = M("employee");

        $result = $employee->where("id=$uid")->setField('pagesize', $pagesize);
        if ($result)
        {
            $this->success('设置成功');
        } else
        {
            $this->error('设置失败');
        }
    }

}

?>